/*
 * Created on 01-feb-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package core;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Hashtable;

import javax.swing.JFrame;

import utils.DBG;
import core.loaders.ResourceLoader;

public class ImageLibrary extends JFrame {

	private static ImageLibrary instance;

	Toolkit toolkit;
	Hashtable recursos;
	Hashtable recursosBuffered;
	ResourceLoader loader;
	boolean iniciada;

	private ImageLibrary() {
		System.out.println(getClass().getResource("/gui/res/cfg/spritesjuego.txt"));
	//	try{System.in.read();}catch(Exception e){}
		loader = new ResourceLoader("/gui/res/cfg/spritesjuego.txt", this);
		recursos = new Hashtable();
		recursosBuffered = new Hashtable();
	}

	public static ImageLibrary getInstance() {
		if (instance == null) {
			instance = new ImageLibrary();
			instance.init();
		}
		return instance;
	}

	public void init() {

		loader.loadImages();
		iniciada = true;
	}

	public Image getDefault() {
		return null;
	}

	public Image getImgNumber(int i) {

		return null;
	}

	public Image getImage(String name) {
		return (Image) recursos.get(name);
	}

	public void load(String ruta, String name) {
		Image img;
		Toolkit toolkit;
		MediaTracker tracker;
		toolkit = this.getToolkit();
		tracker = new MediaTracker(this);
		try{
			System.out.println("ruta>>>>>>>>>>>" + ruta);
		img = toolkit.createImage(new URL(getClass().getResource(ruta).toString()));
		tracker.addImage(img, 0);
		try {
			tracker.waitForAll();
		} catch (Exception e) {
			System.out.println(":: Error loading image :: \n" + e);
		}
		tracker.removeImage(img);
		recursos.put(name, img);
		}catch(Exception e){
			System.out.println("Exception en crear imagen image library linea 76");
		}
		
	}

	public void show() {
		DBG.showMsg(recursos.toString(), this);

	}

	private BufferedImage getBufferedImage(String name) {
		BufferedImage bimg;

		bimg = new BufferedImage(getImage(name).getWidth(null), getImage(name)
				.getHeight(null), BufferedImage.TYPE_INT_RGB);

		return bimg;
	}

	public BufferedImage getBImage(String name) {
		return (BufferedImage) recursosBuffered.get(name);
	}

	public Image createEmptyImage(int xsize, int ysize) {
		Image img = new BufferedImage(xsize, ysize, BufferedImage.TYPE_INT_ARGB);
		return img;
	}

	/**
	 * Returns a scaled version of the image labeled with name. And with new
	 * size as width, height.
	 * 
	 * @param name
	 * @param width
	 * @param height
	 * @return
	 */
	public Image getScaledImage(String name, int width, int height) {
		BufferedImage img = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);
		Image original = getImage(name);

		Graphics2D g2 = (Graphics2D) img.getGraphics();

		g2.scale((double) width / (double) original.getWidth(null),
				(double) height / (double) original.getHeight(null));
		g2.drawImage(original, 0, 0, null);

		return img;
	}
}
